package com.funo.ehealth.util;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;

/**
 * 日志工具类
 * 
 * <hr/>
 * <b>2012-5-24:</b> 新建
 * <hr/>
 * 
 * @author linzhiwei
 * 
 * @version V1.0
 */
public class LogUtil {

	/**
	 * 将pattern字符串中的"{}"占位符,替换成传入的参数params[index]
	 * 
	 * @param log
	 *            日志记录器
	 * @param pattern
	 *            待记录日志的字符串,如: "记录日志,参数:{}"
	 * @param params
	 *            参数数组
	 */
	public static void debug(Log log, String pattern, Object... params) {
		if (null == log || !log.isDebugEnabled()
				|| StringUtils.isBlank(pattern)) {
			return;
		}
		log.debug(handleMsg(pattern, params));
	}

	/**
	 * 将pattern字符串中的"{}"占位符,替换成传入的参数params[index]
	 * 
	 * @param log
	 *            日志记录器
	 * @param pattern
	 *            待记录日志的字符串,如: "记录日志,参数:{}"
	 * @param params
	 *            参数数组
	 */
	public static void info(Log log, String pattern, Object... params) {
		if (null == log || !log.isInfoEnabled() || StringUtils.isBlank(pattern)) {
			return;
		}
		log.info(handleMsg(pattern, params));
	}

	/**
	 * 将pattern字符串中的"{}"占位符,替换成传入的参数params[index]
	 * 
	 * @param log
	 *            日志记录器
	 * @param pattern
	 *            待记录日志的字符串,如: "记录日志,参数:{}"
	 * @param params
	 *            参数数组
	 */
	public static void warn(Log log, String pattern, Object... params) {
		if (null == log || !log.isWarnEnabled() || StringUtils.isBlank(pattern)) {
			return;
		}
		log.warn(handleMsg(pattern, params));
	}

	/**
	 * 将pattern字符串中的"{}"占位符,替换成传入的参数params[index]
	 * 
	 * @param log
	 *            日志记录器
	 * @param e
	 *            抛错
	 * @param pattern
	 *            待记录日志的字符串,如: "记录日志,参数:{}"
	 * @param params
	 *            参数数组
	 */
	public static void error(Log log, Throwable e, String pattern,
			Object... params) {
		if (null == log || !log.isWarnEnabled() || StringUtils.isBlank(pattern)) {
			return;
		}
		log.error(handleMsg(pattern, params), e);
	}

	/**
	 * 将pattern字符串中的"{}"占位符,替换成传入的参数params[index]
	 * 
	 * @param pattern
	 *            待记录日志的字符串,如: "记录日志,参数:{}"
	 * @param params
	 *            参数数组
	 * @return String
	 */
	private static String handleMsg(String pattern, Object[] params) {
		return handleMsg2(pattern, params);
	}

	/**
	 * 先按{}分组,在拼接
	 * 
	 * @param pattern
	 * @param params
	 * @return
	 */
	@SuppressWarnings("unused")
	@Deprecated
	private static String handleMsg1(String pattern, Object[] params) {

		if (null == params || params.length == 0) {
			return pattern;
		}
		String[] strs = StringUtils.split(" " + pattern, "{}");
		StringBuilder sb = new StringBuilder(pattern.length());
		int len = strs.length < params.length ? strs.length : params.length;
		for (int i = 0; i < len; i++) {
			sb.append(strs[i]).append(params[i]);
		}
		for (int i = len; i < strs.length; i++) {
			sb.append(strs[i]);
		}
		return sb.toString();
	}

	/**
	 * 直接替换{}
	 * 
	 * @param pattern
	 * @param params
	 * @return
	 */
	private static String handleMsg2(String pattern, Object[] params) {

		if (null == pattern || pattern.trim().length() == 0) {
			return pattern;
		}

		if (null == params || params.length == 0) {
			return pattern;
		}
		for (int i = 0; i < params.length; i++) {
			if (null == params[i]) {
				params[i] = "null";
			}
			pattern = StringUtils.replaceOnce(pattern, "{}", params[i]
					.toString());
		}

		return pattern;
	}

}
